package foo.bar.blog.radeox;

import foo.bar.radeox.engine.BaseRenderEngine;
import foo.bar.radeox.api.engine.ImageRenderEngine;
import foo.bar.radeox.api.engine.IncludeRenderEngine;
import foo.bar.radeox.api.engine.context.RenderContext;
import foo.bar.blog.web.action.general.ExternalLinkImageActionBean;
import foo.bar.blog.web.action.general.MailToImageActionBean;
import foo.bar.blog.web.action.general.ResourceRetrievalActionBean;
import foo.bar.blog.core.domain.Resource;
import foo.bar.blog.core.services.BlogService;
import net.sourceforge.stripes.util.UrlBuilder;
import net.sourceforge.stripes.integration.spring.SpringBean;

import java.util.Locale;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class BlogRenderEngine extends BaseRenderEngine implements ImageRenderEngine, IncludeRenderEngine {


    public static final String RENDER_CONTEXT_ENTRY_ID_PARAM_KEY = "RENDER_CONTEXT_ENTRY_ID_PARAM_KEY";

    private BlogService blogService;


    public BlogRenderEngine(foo.bar.radeox.api.engine.context.InitialRenderContext initialRenderContext) { 
        super(initialRenderContext);
    }





    // === ImageRenderEngine ======================

    public String getExternalImageLink(RenderContext renderContext) {
        UrlBuilder urlBuilder = new UrlBuilder(Locale.getDefault(), ExternalLinkImageActionBean.class, true);
        urlBuilder.setEvent("view");
        return "<img src='"+getContextPath()+urlBuilder.toString()+"'/>";
    }

    public String getMailToImageLink(RenderContext renderContext) {
        UrlBuilder urlBuilder = new UrlBuilder(Locale.getDefault(), MailToImageActionBean.class, true);
        urlBuilder.setEvent("view") ;
        return "<img src='"+getContextPath()+ urlBuilder.toString()+"'/>";
    }

    // === IncludeRenderEngine ========================

    public String include(String name, RenderContext renderContext) {
        return "";
    }

    public String includeResource(String name, RenderContext renderContext) {
        String entryId = getEntryId(renderContext);

        Resource resource = blogService.getResourceByNameAndEntryId(name, Long.valueOf(entryId));
        UrlBuilder urlBuilder = new UrlBuilder(Locale.getDefault(), ResourceRetrievalActionBean.class, true);
        urlBuilder.setEvent("view");
        urlBuilder.addParameter("entry", entryId);
        urlBuilder.addParameter("resourceName", name);

        if (resource.getMimeType().toLowerCase().startsWith("image")) {
            return "<img src='"+getContextPath()+urlBuilder.toString()+"'/>";
        }
        else {
            return "<a href='"+getContextPath()+urlBuilder.toString()+"'>"+name+"</a>";
        }
    }


    // === commons ==========================================
    protected String getEntryId(RenderContext renderContext) {
        String id = String.valueOf(renderContext.getParameters().get(RENDER_CONTEXT_ENTRY_ID_PARAM_KEY));
        if ((id == null) || (id.trim().length() <= 0)) {
            throw new IllegalArgumentException("Cannot find entry Id from RenderEngine ["+this+"]'s RenderContext ["+renderContext+"] with key ["+ RENDER_CONTEXT_ENTRY_ID_PARAM_KEY +"]");
        }
        return id;
    }

    protected String getContextPath() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
        String contextPath = servletRequestAttributes.getRequest().getContextPath();
        return contextPath;
    }


    @SpringBean("blogService")
    public void setBlogService(BlogService blogService) {
        this.blogService = blogService;
    }
}
